﻿using Asptcp.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using System;
using System.Reflection.Metadata.Ecma335;
using System.Threading.Tasks;

namespace Asptcp.Simple
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            IHost host = CreateHostBuilder(args).Build();
            await host.RunAsync();
            host.Dispose();
            //try
            //{
            //    await host.StartAsync();
            //    host.Services.GetService<TestService>().Say("GetService调用Say");
            //    while (true)
            //    {
            //        await Task.Delay(1);
            //    }
            //    await host.StopAsync(TimeSpan.FromSeconds(3));
            //}
            //catch(Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //}
            //finally
            //{
                
            //    host.Dispose();
            //}
        }
        static IHostBuilder CreateHostBuilder(string[] args)
        {
            IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args)
                .ConfigureServices((_, services) =>
                    {
                        services.AddHostedService<AspTcpHostService>();
                        //Console.WriteLine("服务配置");
                        services.AddSingleton<AsptcpServer>(m => new AsptcpServer(System.Net.IPAddress.Parse("0.0.0.0"), 1234));
                        services.AddScoped<TestService>();
                        //services.AddHostedService<Worker>()
                        //.AddScoped<IMessageWriter, MessageWriter>()
                    }
                );
            return hostBuilder;
        }
    }
}
